
import http from '@/http';

const state = {
  deviceTypeList: [],
  deviceTypeMap: {},
  getDeviceIcons: (type) => {
    const list = [{
      value: 'NetDevices',
      img: require('./nodeIcon/network.png'),
      label: '网络设备'
    }, {
      value: 'Sequipment',
      img: require('./nodeIcon/security-equip.png'),
      label: '安防设备'
    }, {
      value: 'VirtualMachine',
      img: require('./nodeIcon/virtual.png'),
      label: '虚拟机'
    }, {
      value: 'Server',
      img: require('./nodeIcon/server.png'),
      label: '服务器'
    }, {
      value: 'Application',
      img: require('./nodeIcon/safe.png'),
      label: '安防系统'
    }, {
      value: 'Storage',
      img: require('./nodeIcon/cloud.png'),
      label: '云平台'
    }, {
      value: 'HostMachine',
      img: require('./nodeIcon/node.png'),
      label: '宿主机'
    }, {
      value: 'HostingVirtualMachines',
      img: require('./nodeIcon/cloud-virtual.png'),
      label: '云平台承载虚拟机'
    }, {
      value: 'Router',
      img: require('./nodeIcon/wifi.png'),
      label: '路由器'
    }, {
      value: 'Network',
      img: require('./nodeIcon/switch.png'),
      label: '交换机'
    }, {
      value: 'Network2',
      img: require('./nodeIcon/switch2.png'),
      label: '交换机2'
    }, {
      value: 'Network3',
      img: require('./nodeIcon/switch3.png'),
      label: '交换机3'
    }, {
      value: 'IOT',
      img: require('./nodeIcon/host.png'),
      label: '网关'
    }, {
      value: 'Security',
      img: require('./nodeIcon/fire-wall.png'),
      label: '防火墙'
    }, {
      value: 'IDS',
      img: require('./nodeIcon/IDS.png'),
      label: 'IDS'
    }]
    return (list.find(node => {
      return node.value === type
    }) || list[0]).img
  }
}
const actions = {
  // updateDeviceTypes({ commit, state }) {
  updateDeviceTypes({ commit }) {
    return new Promise((resolve, reject) => {
      http.get('/device/getDeviceTypeList').then(res => {
        const list = (res || []).map(item => {
          item.children = (item.children || []).map(child => {
            if (!child.children?.length) {delete child.children}
            return child
          })
          return item
        })
        resolve(list)
        const map = list.reduce((obj, cur) => {
          const children = (cur.children || []).reduce((child, curChild) => {
            return {
              ...child,
              [curChild.key]: curChild.keyName
            }
          }, {
            [cur.key]: cur.keyName
          })
          return {
            ...obj,
            ...children
          }
        }, {})
        commit('deviceTypeList', list);
        commit('deviceTypeMap', map);
      }).catch(err => {
        reject(err)
      })
    })
  }
}
const mutations = {
  deviceTypeList(state, list) {
    state.deviceTypeList = [...list]
  },
  deviceTypeMap(state, update) {
    state.deviceTypeMap = { ...update }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
};